package com.banmao.mall.controller.admin;

import com.banmao.mall.model.bo.admin.system.AddPermissionBO;
import com.banmao.mall.model.bo.admin.system.AddRoleBO;
import com.banmao.mall.model.bo.admin.system.DeleteAdminBO;
import com.banmao.mall.model.bo.admin.system.UpdateAdminBO;
import com.banmao.mall.model.pojo.Role;
import com.banmao.mall.model.pojo.Storage;
import com.banmao.mall.model.vo.BaseRespVO;
import com.banmao.mall.model.vo.admin.market.FileUploadVO;
import com.banmao.mall.model.vo.admin.system.*;
import com.banmao.mall.service.admin.SystemService;
import org.apache.commons.beanutils.PropertyUtilsBean;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;


@RestController
@RequestMapping("/admin")
public class SystemController {
    @Autowired
    SystemService systemService;

    @RequiresPermissions("admin:admin:list")
    @RequestMapping("admin/list")
    public BaseRespVO adminListVO(Integer page,Integer limit,String sort,String order,String username){
        //page=1&limit=20&sort=add_time&order=desc
        //order desc 排序顺序

        AdminListVO adminListVO = systemService.selectAdminList(page,limit,sort,order,username);

        return BaseRespVO.ok(adminListVO);

    }

//    @RequiresPermissions("admin:role:options")
    @RequestMapping("role/options")
    public BaseRespVO adminListOption(){
        List<RoleVO> list =  systemService.selectRoleIdAndName();
        return BaseRespVO.ok(list);
    }
    @RequiresPermissions("admin:admin:create")
    @RequestMapping("/admin/create")
    public BaseRespVO addcreate(@RequestBody AddAdminVO addAdminVO){
        /*
            avatar: "http://182.92.235.201:8083/wx/storage/fetch/ez3jpsdk7mxuc4x86vxh.jpg"
            password: "admin1234"
            roleIds: [1, 2, 3]
            username: "admin1234"
         */
        if(addAdminVO.getPassword().length()<6){
            return BaseRespVO.error("密码长度小于6",500);
        }
        AddRoleVO2 addRoleVO2 = systemService.addCreate(addAdminVO);
        return BaseRespVO.ok(addRoleVO2);
    }
    @RequiresPermissions("admin:admin:delete")
    @RequestMapping("admin/delete")
    public BaseRespVO deleteAdmin(@RequestBody DeleteAdminBO deleteAdminBO){
        /**
         * avatar : http://localhost:8083/static/images/1/0/9/c/5/7/c/2/88939209-8a86-456b-8338-eafe12377220-a.jpg
         * id : 9
         * roleIds : [2,3,1]
         * username : admin12345
         */
        Integer id = deleteAdminBO.getId();
        systemService.deleteAdminById(id);
        return BaseRespVO.ok(null);
    }
    @RequiresPermissions("admin:admin:update")
    @RequestMapping("admin/update")
    public BaseRespVO updateAdmin(@RequestBody UpdateAdminBO updateAdminBO){
        AddRoleVO2 addRoleVO2  = systemService.updateAdmin(updateAdminBO);
        return BaseRespVO.ok(addRoleVO2);
    }
    @RequiresPermissions("admin:log:list")
    @RequestMapping("log/list")
    public BaseRespVO selectLogList(Integer page,Integer limit,String sort,String order,String name){
        LogListVO logListVO = systemService.selectLogList(page,limit,sort,order,name);
        return BaseRespVO.ok(logListVO);
    }

    @RequiresPermissions("admin:storage:list")
    @RequestMapping("storage/list")
    public BaseRespVO selectStorageList(Integer page,Integer limit,String sort,String order,String key,String name){
        StorageListVO storageListVO = systemService.selectStorageList(page,limit,sort,order,key,name);
        return BaseRespVO.ok(storageListVO);
    }

    @RequiresPermissions("admin:storage:update")
    @RequestMapping("storage/update")
    public BaseRespVO updateStorage(@RequestBody Storage storage){

        systemService.updateStorage(storage);
        return BaseRespVO.ok(null);
    }
    @RequiresPermissions("admin:storage:delete")
    @RequestMapping("storage/delete")
    public BaseRespVO updateStorageDeleted(@RequestBody Storage storage){
        systemService.updateStorageDeleted(storage);
        return BaseRespVO.ok(null);
    }
    @RequiresPermissions("admin:role:list")
    @RequestMapping("role/list")
    public BaseRespVO selectRoleList(Integer page,Integer limit,String sort,String order,String name){
        RoleListVO roleListVO = systemService.selectRoleList(page,limit,sort,order,name);
        return BaseRespVO.ok(roleListVO);
    }
    @RequiresPermissions("admin:role:create")
    @RequestMapping("role/create")
    public BaseRespVO createRole(@RequestBody AddRoleBO addRoleBO){
        Role role = systemService.createRole(addRoleBO);
        return BaseRespVO.ok(role);
    }
    @RequiresPermissions("admin:role:delete")
    @RequestMapping("role/delete")
    public BaseRespVO deleteRole(@RequestBody Role role){
        systemService.deleteRole(role);
        return BaseRespVO.ok(null);
    }

    @RequiresPermissions("admin:role:update")
    @RequestMapping("role/update")
    public BaseRespVO updateRole(@RequestBody Role role){
        systemService.updateRole(role);
        return BaseRespVO.ok(null);
    }

    @RequestMapping("noperm")
    public BaseRespVO noperm(){
        return BaseRespVO.error("没有权限",500);
    }
    @RequiresPermissions("admin:role:permissions:get")
    @RequestMapping(value = "role/permissions",method = RequestMethod.GET)
    public BaseRespVO selectRolePermissions(Integer roleId){
        PermissionVO permissionVO = systemService.selectRolePermissions(roleId);
        return BaseRespVO.ok(permissionVO);
    }
    @RequiresPermissions("admin:role:permission:update")
    @RequestMapping(value = "role/permissions",method = RequestMethod.POST)
    public BaseRespVO selectRolePermissions(@RequestBody AddPermissionBO addPermissionBO){
        systemService.updateRolePermissions(addPermissionBO);
        return BaseRespVO.ok(null);
    }
}
